import { reqGoodsInfo, reqAddOrUpdateShopCar } from "@/api";
import { getUUID } from "@/utils/uuid_token";

const state = {
  goodsInfo: {},
  uuid_token: getUUID(),
};
const mutations = {
  GETGOODSINFO(state, goodsInfo) {
    state.goodsInfo = goodsInfo;
  },
};
const actions = {
  async getGoodsInfo(context, skuid) {
    let result = await reqGoodsInfo(skuid);
    if (result.code == 200) {
      context.commit("GETGOODSINFO", result.data);
    }
  },
  async addOrUpdateShopCar(context, { skuId, skuNum }) {
    let result = await reqAddOrUpdateShopCar(skuId, skuNum);
    if (result.code == 200) {
      return "ok";
    } else {
      return Promise.reject(new Error("fail"));
    }
  },
};
const getters = {
  categoryView(state) {
    return state.goodsInfo.categoryView || {};
  },
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {};
  },
  spuSaleAttrList(state) {
    return state.goodsInfo.spuSaleAttrList || [];
  },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
